﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Passwords.Models;
using Silvermoon.Core;
using Silvermoon.Controls.Forms;
using Passwords.Pages;
using Passwords.DAL;
using System.Windows.Forms;
using System.Threading;
using Silvermoon.Controls.Base;
using Silvermoon.UI;
using Silvermoon.Debug;
using Silvermoon.Navigation;
using System.Diagnostics;
using Passwords.Biz;
using Silvermoon.Controls;

namespace Passwords.Controllers
{
    public class LoginController : Controller<LoginPage, int>
    {

        public LoginController(LoginPage view, int model)
            : base(view, model)
        {
        }

        public override void UpdateView()
        {
        }

        protected override void RegisterCommands()
        {
            RegisterCommand(LoginPage.OkCommand, (s, e) => OnOk());
            RegisterCommand(LoginPage.CancelCommand, (s, e) => OnCancel());
            RegisterCommand(PasswordCommands.Debug, (s, e) => OnDebug());
        }

        private void OnDebug()
        {
            DebuggerWindow.Monitor = false;
            DebuggerWindow.ShowDebugInformation();
        }




        private void OnCancel()
        {
            MessageDialog.Show("Exit application?", "", View.Screen.Close, View.FocusInput);
        }

        private void OnOk()
        {
            View.IsKeyboardVisible = false;
            View.Enabled = false;
            while (View.KeyboardVisibility != 0f)
            {
                Silvermoon.Core.Screen.Current.Progress();
                Thread.Sleep(1000 / 30);
            }
            DebuggerWindow.Monitor = true;

            View.EnqueueThread(CheckPasswordThread, View.Password);
//            ThreadPool.QueueUserWorkItem(CheckPasswordThread, View.Password);
            View.Password = string.Empty;
        }


        private void CheckPasswordThread(object o)
        {
            string password = o as string;
            bool isPasswordCorrect = Context.CheckPassword(password);

            if (isPasswordCorrect)
            {
                Context.Create(password);
                DebuggerWindow.Monitor = true;
                View.Invoke(() =>
                    {
                        PasswordsPage nav = new PasswordsPage();
                        View.NavigateTo(nav);
                    });
            }
            else
            {
                View.Invoke(() =>
                {
                    View.Enabled = true;
                    MessageDialog.Show("Error", "Wrong Password", "Retry", "Exit", OnWrongPasswordOK, OnWrongPasswordCancel);
                });
            }
        }


        void OnWrongPasswordOK()
        {
            View.Enabled = true;
            View.Password = string.Empty;
            View.FocusInput();
        }

        void OnWrongPasswordCancel()
        {
            View.Visible = false;
            View.SetTransition(0, TransitionMask.None, TransitionMask.None);
            View.Hide();
            View.Dispose();
            Silvermoon.Core.Screen.Current.Close();
        }

    }
}